package com.example.human_resource_management.controller;


import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.SalaryEmployeeDto;
import com.example.human_resource_management.entry.po.Employee;
import com.example.human_resource_management.entry.po.Salary;
import com.example.human_resource_management.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/salary")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;


    //查询员工自己的薪资
    @GetMapping("gz")
    public ResponseData<Salary> getSalary() {
        Long empId = UserContextUtils.getEmpId();
        if (empId != null) {
            Salary salary = salaryService.getSalaryById(empId);
            if (salary != null) {
                return ResponseData.ok(salary);
            }
        }
        return ResponseData.warn("未获取到员工信息");
    }


//管理员端
// 联查员工和薪资信息
@GetMapping("/list")
public ResponseData<List<SalaryEmployeeDto>> selectSalaryEmployeeList() {
    List<SalaryEmployeeDto> list = salaryService.selectSalaryEmployeeList();
    return ResponseData.ok(list);
}

    // 新增薪资信息
    @PostMapping("/add")
    public ResponseData<String> addSalary(@RequestBody Salary salary) {
        boolean success = salaryService.save(salary);
        return success ? ResponseData.ok("新增薪资信息成功") : ResponseData.warn("新增薪资信息失败");
    }

    // 修改薪资信息
    @PutMapping("/update")
    public ResponseData<String> updateSalary(@RequestBody Salary salary) {
        boolean success = salaryService.updateById(salary);
        return success ? ResponseData.ok("修改薪资信息成功") : ResponseData.warn("修改薪资信息失败");
    }

    // 删除薪资信息
    @DeleteMapping("/delete/{salaryId}")
    public ResponseData<String> deleteSalary(@PathVariable Long salaryId) {
        boolean success = salaryService.removeById(salaryId);
        return success ? ResponseData.ok("删除薪资信息成功") : ResponseData.warn("删除薪资信息失败");
    }
}
